package team.yidong.fenda.serviceImpl;

import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.DTO.HospitalActivityDTO;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.HospitalActivity;
import team.yidong.fenda.entity.HospitalActivityImg;
import team.yidong.fenda.entity.HospitalProfileImg;
import team.yidong.fenda.repository.HospitalActivityImgRepository;
import team.yidong.fenda.repository.HospitalActivityRepository;
import team.yidong.fenda.service.HospitalActivityService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuanlong on 2018/3/6.
 */
@Service
public class HospitalActivityServiceImpl implements HospitalActivityService {
    @Autowired
    HospitalActivityRepository hospitalActivityRepository;
    @Autowired
    HospitalActivityImgRepository hospitalActivityImgRepository;

    @Override
    public JSONArray findHospitalActivity(Integer hospitalId) {

        List<HospitalActivityDTO> hospitalActivityDTOList = new ArrayList<>();
        List<HospitalActivity> hospitalActivityList = hospitalActivityRepository.findActivityListByHospitalId(hospitalId);

        for (HospitalActivity hospitalActivity : hospitalActivityList) {
            HospitalActivityDTO hospitalActivityDTO = new HospitalActivityDTO();
            hospitalActivityDTO.setContent(hospitalActivity.getActivityContent());
            List<String> imgPathList = hospitalActivityImgRepository.findHospitalActivityImgPathByActivityId(hospitalActivity.getId());
            hospitalActivityDTO.setImgPathList(imgPathList);
            hospitalActivityDTO.setHeadline(hospitalActivity.getHeadLine());

            hospitalActivityDTOList.add(hospitalActivityDTO);
        }
        JSONArray jsonArray = JSONArray.fromObject(hospitalActivityDTOList);
        return jsonArray;
    }

    @Transactional
    @Override
    public void addHospitalActivity(Integer hospitalId, String content, String headline, List<String> imgPathList) {
        HospitalActivity hospitalActivity = new HospitalActivity();
        hospitalActivity.setHeadLine(headline);
        hospitalActivity.setActivityContent(content);
        hospitalActivity.setHospitalId(hospitalId);
        hospitalActivity.setStatus("use");
        hospitalActivityRepository.save(hospitalActivity);
        for (String imgPath : imgPathList) {
            HospitalActivityImg hospitalActivityImg = new HospitalActivityImg();
            hospitalActivityImg.setImgPath(imgPath);
            hospitalActivityImg.setHospitalActivityId(hospitalActivity.getId());
            hospitalActivityImg.setStatus("use");
            hospitalActivityImgRepository.save(hospitalActivityImg);
        }
    }

    @Transactional
    @Override
    public void deleteHospitalActivity(Integer hospitalId) {

    }

    @Transactional
    @Override
    public void updateHospitalActivity(Integer hospitalId, String headline, String content, List<String> imgPathList) {
        List<HospitalActivity> hospitalActivityList = hospitalActivityRepository.findActivityListByHospitalId(hospitalId);
        for (HospitalActivity hospitalActivity : hospitalActivityList) {
            hospitalActivity.setStatus("overdue");
            hospitalActivityRepository.save(hospitalActivity);
            List<HospitalActivityImg> hospitalActivityImgList = hospitalActivityImgRepository.findHospitalActivityImgByActivityId(hospitalActivity.getId());
            for (HospitalActivityImg hospitalActivityImg : hospitalActivityImgList) {
                hospitalActivityImg.setStatus("overdue");
                hospitalActivityImgRepository.save(hospitalActivityImg);
            }
        }
        HospitalActivity hospitalActivity = new HospitalActivity();
        if (headline != null) {
            hospitalActivity.setHeadLine(headline);
        }
        if (content != null) {
            hospitalActivity.setActivityContent(content);
        }

        hospitalActivity.setHospitalId(hospitalId);
        hospitalActivity.setStatus("use");
        hospitalActivityRepository.save(hospitalActivity);
        for (String imgPath : imgPathList) {
            HospitalActivityImg hospitalActivityImg = new HospitalActivityImg();
            hospitalActivityImg.setImgPath(imgPath);
            hospitalActivityImg.setHospitalActivityId(hospitalActivity.getId());
            hospitalActivityImg.setStatus("use");
            hospitalActivityImgRepository.save(hospitalActivityImg);
        }
    }
}
